import apiClient from "../apiClient";
import { PageQuery } from "@/types/api";

export interface Notice {
	id: number;
	title: string;
	content: string;
	status: number;
	createtime: number;
	updatetime: number;
	is_home: number;
}

export interface ApiResponse<T> {
	status: number;
	message: string;
	time: string;
	data: {
		items: T[];
		total: number;
	};
}

export interface NoticeQuery extends PageQuery {
	name?: string;
	status?: number;
}

export interface NoticeForm extends Pick<Notice, "title" | "content" | "status" | "is_home"> {}

enum Api {
	NoticeList = "/notifications/index",
	NoticeAdd = "/notifications/add",
	NoticeEdit = "/notifications/edit",
	NoticeDelete = "/notifications/del",
}

export class NoticeService {
	/**
	 * 获取通知列表
	 */
	async noticeList(params: NoticeQuery) {
		return apiClient.get<ApiResponse<Notice>>({
			url: Api.NoticeList,
			params,
		});
	}

	/**
	 * 添加通知
	 */
	async noticeAdd(data: NoticeForm) {
		return apiClient.post<ApiResponse<void>>({
			url: Api.NoticeAdd,
			data,
		});
	}

	/**
	 * 编辑通知
	 */
	async noticeEdit(id: number, data: Partial<NoticeForm>) {
		return apiClient.post<ApiResponse<void>>({
			url: Api.NoticeEdit,
			data: { id, ...data },
		});
	}

	/**
	 * 删除通知
	 */
	async noticeDelete(id: number) {
		return apiClient.post<ApiResponse<void>>({
			url: Api.NoticeDelete,
			data: { id },
		});
	}

	/**
	 * 更新通知状态
	 */
	async noticeChangeStatus(id: number, status: number) {
		return this.noticeEdit(id, { status });
	}
}

export const noticeService = new NoticeService();
